<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-select v-model="search.sugType" style="width:100%" placeholder="反馈类型" clearable>
            <el-option :key="config.suggestion.FUNC" label="功能问题" :value="config.suggestion.FUNC"></el-option>
            <el-option :key="config.suggestion.MONE" label="充值问题" :value="config.suggestion.MONE"></el-option>
            <el-option :key="config.suggestion.OTHER" label="其他问题" :value="config.suggestion.OTHER"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getSuggestionsList">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column label="消息类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sugType == config.suggestion.FUNC">功能问题</el-tag>
            <el-tag v-else-if="scope.row.sugType == config.suggestion.MONE">充值问题</el-tag>
            <el-tag v-else-if="scope.row.sugType == config.suggestion.OTHER">其他问题</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="反馈会员">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.usersVo != undefined">
              {{scope.row.usersVo.nickName}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sugContent" label="反馈内容"></el-table-column>
        <el-table-column prop="sugReply" label="回复内容"></el-table-column>
        <el-table-column label="回复时间">
          <template slot-scope="scope">{{scope.row.replyTime | datetime}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-position"
              circle
              title="回复"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="反馈回复" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
          <el-form-item label="反馈问题">
            <el-input v-model="form.sugContent" autocomplete="off" readonly></el-input>
          </el-form-item>
          <el-form-item label="回复" prop="sugReply">
            <el-input type="textarea" :row="4" v-model="form.sugReply" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">回复</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import { config } from "assets/js/Data";

import {
  getSuggestionsList,
  deleteSuggestions,
  updateSuggestionsById
} from "api/SuggestionsApi";

export default {
  name: "SuggestionList",
  data() {
    return {
      search: {
        currentIndex: 1,
        sugType: ""
      },
      config: config,
      tableData: [],
      listNum: 1,
      form: {
        id: "",
        sugContent: "",
        sugReply: ""
      },
      rules: {
        sugReply: [{ required: true, message: "请输入回复内容" }]
      },
      dialogFormVisible: false
    };
  },
  methods: {
    getSuggestionsList() {
      getSuggestionsList(this.search).then(res => {
        this.tableData = res.datas;
        this.listNum = res.pagination.totalCount;
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getNoticeList();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          updateSuggestionsById(this.form).then(res => {
            if (res) {
              this.$base.showOk();
              this.dialogFormVisible = false;
              this.getSuggestionsList();
            }
          });
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteSuggestions({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getSuggestionsList();
          }
        });
      });
    }
  },
  created() {
    this.getSuggestionsList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
      }
    }
  }
};
</script>

<style lang="stylus" scoped></style>